#pragma once
#include"TcpServer.hpp"
#include<pthread.h>
#include"Protocol.hpp"
#include<signal.h>
#include"Task.hpp" 
#include"ThreadPool.hpp"

namespace wyl
{
    #define PORT 8081
    class HttpServer
    {
    public:
        HttpServer(int port = PORT) : _port(port) , _is_stop(false)
        {}
        ~HttpServer(){};
        void Loop()
        {
            TcpServer* tcpser = TcpServer::getinstance(_port);  
            LOG(INFO,"http server begin loop...");
            //提供服务
            while(!_is_stop)
            {
                //获取sock
                struct sockaddr_in browser;
                memset(&browser,0,sizeof(browser));
                socklen_t len = sizeof(browser);
                //accept接收
                int new_sock = accept(tcpser->GetSock(),(struct sockaddr*)&browser,&len);
                if(new_sock < 0 )
                {
                    //连接失败
                    continue;
                }
                
                //构建一个任务
                Task t(new_sock) ; 
                //任务推送进线程池
                ThreadPool::GetInstance()->TaskPush(t);
            }
        }

        void InitServer()
        {
            //管道信号忽略
            signal(SIGPIPE,SIG_IGN) ; 
            LOG(INFO,"http server init... success");
        }
    private:
        bool _is_stop; //服务是否在运行
        uint16_t _port; //端口
    };
}
